/******************************************************************************
 * (c) 2011 Komersi Software                                                  *
 * www.komersisoftware.com                                                    *
 *                                                                            *
 * This file is part of Tadah                                                 *
 * Licensed under GNU General Public License v2.0                             *
 * Check LICENSE.TXT for more information                                     *
 ******************************************************************************/

#include "column.h"
#include "debug.h"
#include "validation.h"
#include "definitions.h"

#include <stdlib.h>
#include <string.h>

int column_list_add(ddl_column_t **list, ddl_column_t *item)
{
   ddl_column_t *cur = NULL;

   if (item == NULL)
   {
	   return FAILURE;
   }

   debug_print_ddl_column(item);
  
   if (*list == NULL)
   {
      *list = item;
      return SUCCESS;
   }

   cur = *list;
  
   while (cur->next != NULL)
      cur = cur->next;
  
   cur->next = item;

   return SUCCESS;
}

#ifndef _MSC_VER
inline
#endif
void set_column_as_primary_index(ddl_column_t *item)
{
	if (item != NULL)
	{
		item->primary_index = IsPrimaryIndex;
	}
}

ddl_column_t *new_ddl_column(char *name, data_type_t type, int size, primary_index_type_t primary_index)
{
  ddl_column_t *column = NULL;

  if (!is_column_name_valid(name))
  {
	  return column;
  }

  column = malloc(sizeof(ddl_column_t));
  if (column == NULL) return NULL;
  
  memset(column, 0, sizeof(ddl_column_t));
  
  strcpy(column->name, name);
  column->type = type;
  column->size = size;
  column->primary_index = primary_index;

  column->next = NULL;
  
  return column;
}

void dispose_column_list(ddl_column_t **list)
{
   ddl_column_t *cur = *list;
   ddl_column_t *aux = *list;

   if (list == NULL)
       return;

   while (cur != NULL)
   {
       aux = cur->next;
       free(cur);
       cur = aux;
   }
}

